package com.lizemin.hippo4jdemo.controller;

import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author lzm
 * @date 2025/5/24 16:36
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    @Qualifier("messageProducePool")
    private ThreadPoolExecutor messageProducePool;

    @Autowired
    @Qualifier("messageConsumePool")
    private DynamicThreadPoolWrapper messageConsumePool;

    @GetMapping("/heartBeat")
    public String heartBeat() {
        return "OK";
    }

    @GetMapping("/getUsers")
    public String getUsers() {
        messageProducePool.execute(() -> {
            log.info("开始从数据库中查询用户信息");
            try {
                TimeUnit.DAYS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        return "success";
    }

    @GetMapping("/updateUsers")
    public String updateUsers() {
        messageConsumePool.execute(() -> {
            log.info("开始从数据库中更新用户信息");
            try {
                TimeUnit.DAYS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        return "success";
    }

}
